<template>
  <div class="login-container">
    <q-card class="login-card">
      <q-card-section>
        <div class="text-h6">系统登录</div>
      </q-card-section>

      <q-card-section>
        <q-form @submit="onSubmit" class="q-gutter-md">
          <q-input
            v-model="form.username"
            label="用户名"
            :rules="[(val) => !!val || '请输入用户名']"
          />

          <q-input
            v-model="form.password"
            label="密码"
            type="password"
            :rules="[(val) => !!val || '请输入密码']"
          />

          <div>
            <q-btn label="登录" type="submit" color="primary" class="full-width" />
          </div>
        </q-form>
      </q-card-section>
      只是演示，随便输入
    </q-card>
  </div>
</template>

<script setup>
defineOptions({
  name: 'UserLogin',
})

import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from 'stores/useUserStore'

const router = useRouter()
const userStore = useUserStore()

const form = ref({
  username: '123',
  password: '123456',
})

const onSubmit = async () => {
  try {
    await userStore.login(form.value)
    await router.push('/dashboard')
  } catch (error) {
    console.error('登录失败:', error)
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f5f5;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 20px;
}
</style>
